var jQT = $.jQTouch({
   icon: 'kilo.png',
   statusBar: 'black'
});

$(document).ready(function(){
	$('#settings form').submit(saveSettings);
	$('#settings').bind('pageAnimationStart', loadSettings);
	var shortName = 'iDouban';
	var version = '0.1';
	var displayName = 'iDouban';
	var maxSize = 65536;
	db = openDatabase(shortName, version, displayName, maxSize);
	db.transaction(
		function(transaction) {
			transaction.executeSql(
			'CREATE TABLE IF NOT EXISTS idbsettings ' +
			' (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, ' +
			' skey TEXT NOT NULL, svalue TEXT NOT NULL); '
			);
			
		}
	);
});
function doGetAccesstoken(){
    // 初始化service
    var service = $.douban({ apiKey: 'blah', apiSecret: 'blah' });

    // 获取未授权的request token
    var requestToken = service.getRequestToken();

    // 用request token获取授权页面
    var authorizationUrl = service.getAuthorizationUrl(requestToken, 'http://blog.luliban.com');
    // 让用户去authorizationUrl页面进行认证。。。

    // 用已授权的request token换取access token
    var accessToken = service.getAccessToken(requestToken);

    // 用户已授权，弹出true
    alert(service.isAuthenticated());
}
function saveSettings() {
	localStorage.age = $('#username').val();
	localStorage.budget = $('#pword').val();
	localStorage.weight = $('#accesstoken').val();
	jQT.goBack();
	return false;
}
function loadSettings() {
	$('#username').val(localStorage.age);
	$('#pword').val(localStorage.budget);
	$('#accesstoken').val(localStorage.weight);
}

function createEntry() {
	var date = sessionStorage.currentDate;
	var calories = $('#calories').val();
	var food = $('#food').val();
	db.transaction(
		function(transaction) {
			transaction.executeSql(
				'INSERT INTO entries (date, calories, food) VALUES (?, ?, ?);',
				[date, calories, food],
				function(){
					refreshEntries();
					jQT.goBack();
				},
				errorHandler
			);
		}
	);
	return false;
}
